package com.hopu.petshop.filter;

import com.hopu.petshop.service.PetTypeService;
import com.hopu.petshop.service.impl.PetTypeServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.SQLException;

/**
 * 查询所有宠物类型的过滤器
 */
//@WebFilter({"/addPet.html","/modifyPet.html","/findPet.do"})
public class FindAllPetTypesFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        //查询宠物类型
        PetTypeService petTypeService = new PetTypeServiceImpl();
        try {
            req.setAttribute("petTypes",petTypeService.findAllPetTypes());
            //跳转添加宠物jsp
            filterChain.doFilter(servletRequest, servletResponse);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroy() {

    }
}
